package com.asset.dispoal.poi.documentation.util;

import com.asset.dispoal.poi.documentation.domain.db.ConfItemDetailDo;
import com.asset.dispoal.poi.documentation.domain.db.ConfItemDo;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.policy.DynamicTableRenderPolicy;
import com.deepoove.poi.policy.TableRenderPolicy;
import com.deepoove.poi.util.TableTools;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.util.List;

public class DefineMethodPolicy extends DynamicTableRenderPolicy {

    @Override
    public void render(XWPFTable table, Object data) throws Exception {
        List<ConfItemDo> doList = (List<ConfItemDo>) data;
        for (int i = doList.size() - 1; i >= 0; i--) {
            ConfItemDo item = doList.get(i);
            for (int j = item.getConfItemDetailDtoList().size() - 1; j >= 0; j--) {
                ConfItemDetailDo detailItem = item.getConfItemDetailDtoList().get(j);
                XWPFTableRow newRow = table.insertNewTableRow(1);
                for (int z = 0; z < 5; z++) {
                    newRow.createCell();
                }
                RowRenderData serverData = Rows.of(item.getIndex() + "", item.getClassifyName(),
                        detailItem.getTypeName(), item.getPurpose(), detailItem.getSourceForm()).center().create();
                TableRenderPolicy.Helper.renderRow(newRow, serverData);
            }
        }

        int index = 0;
        for (int i = 0; i < doList.size(); i++) {
            int size = doList.get(i).getConfItemDetailDtoList().size();
            if (size > 1) {
                //代表需要合并的才进行合并
                TableTools.mergeCellsVertically(table, 0, index + 1, index + size);
                TableTools.mergeCellsVertically(table, 1, index + 1, index + size);
                TableTools.mergeCellsVertically(table, 3, index + 1, index + size);
                index = index + size;
            } else {
                index = index + 1;
            }
            for (int j = 0; j < 5; j++) {
                XWPFTableCell cell = table.getRow(i + 1).getCell(j);
                cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
            }
        }
    }
}
